/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.entiti;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.entiti.Pasien;
import model.dao.iface.PasienIface;
import model.dao.implement.PasienImplement;

/**
 *
 * @author compaq
 */
public class ControlPasien extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private PasienIface daoPasien ;

    public ControlPasien() {
        this.daoPasien = new PasienImplement();
    }
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here 
           //List<Pasien> list = daoPasien.bacaSemuaPasien();
            Pasien p = daoPasien.bacaPasien("123456") ;
          
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>" + p.getNama() + "</h1>");
            out.println("</body>");
            out.println("</html>");
          */ 
          List<Pasien> p = daoPasien.bacaSemuaPasien();
          request.setAttribute("daftarPasien", p);
          RequestDispatcher rd = request.getRequestDispatcher("/tampil.jsp");
          rd.forward(request, response);
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        String aksi = request.getParameter("aksi") ;
        if(aksi!=null){
            if(aksi.equalsIgnoreCase("baca")){
                Pasien p = daoPasien.bacaPasien(id);
                //List<Pasien> list = new ArrayList<Pasien>();
                request.setAttribute("daftarPasien", p);
                RequestDispatcher rd = request.getRequestDispatcher("/edit.jsp");
                rd.forward(request, response);
            }
            else if(aksi.equalsIgnoreCase("hapus")){
                daoPasien.hapusPasien(id);
                 processRequest(request, response);
            }
        }
        else{
            processRequest(request, response);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        String aksi = request.getParameter("flag") ;
         Pasien p = new Pasien() ;
        if(aksi.equalsIgnoreCase("tambah")){         
            p.setId(request.getParameter("id")) ;
            p.setNama(request.getParameter("nama")) ;
            p.setUmur(request.getParameter("umur")) ;
            p.setJenkel(request.getParameter("jenkel")) ;
            p.setGoldarah(request.getParameter("goldarah")) ;
            p.setAlamat(request.getParameter("alamat")) ;
            daoPasien.tambahPasien(p) ;
        }
        else if(aksi.equalsIgnoreCase("hapus")){  
            p.setNama(request.getParameter("nama")) ;
            p.setUmur(request.getParameter("umur")) ;
            p.setJenkel(request.getParameter("jenkel")) ;
            p.setGoldarah(request.getParameter("goldarah")) ;
            p.setAlamat(request.getParameter("alamat")) ;
            id = request.getParameter("id") ;
            daoPasien.ubahPasien(id,p) ;          
        }
        
        
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
